﻿using System;
using System.Windows.Forms;
using WinForm.Helper;

namespace WinForm.Common
{
    /// <summary>
    /// 刷新datagridview的代理
    /// </summary>
    public delegate void RefreshDataSource();

    public partial class XPagingBar : UserControl
    {
        public XPagingBar()
        {
            InitializeComponent();
            PageIndex = 1;
            PageSize = 20;
        }

        public event RefreshDataSource xRefresh;

        /// <summary>
        /// 每页显示数目
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecordCount { get; set; }

        /// <summary>
        /// 页面数（totalRecordCount/pageSize+1)
        /// </summary>
        public int PageCount { get; set; }

        /// <summary>
        /// 当前页面数
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 绑定
        /// </summary>
        public void Bind()
        {
            if(TotalRecordCount==0)
            {
                PageCount = 1;
            }else
            {
                PageCount = TotalRecordCount / PageSize + 1;   
            }
            if (PageIndex > PageCount)
            {
                this.PageIndex = this.PageCount;
            }
            if (PageCount == 1)
            {
                PageIndex = 1;
            }
            lbTotalRecords.Text = TotalRecordCount.ToString();
            lbPageIndex.Text = PageIndex.ToString();
            lbPageSize.Text = PageSize.ToString();
            txtPage.Text = lbPageIndex.Text;
            if(PageIndex>1)
            {
                bnPrevious.Enabled = true;
                bnFirst.Enabled = true;
            }else
            {
                bnPrevious.Enabled = false;
                bnFirst.Enabled = false;
            }

            if(PageIndex<PageCount)
            {
                bnNext.Enabled = true;
                bnLast.Enabled = true;
            }else
            {
                bnNext.Enabled = false;
                bnLast.Enabled = false;
            }

            if (TotalRecordCount == 0)
            {
                bnPrevious.Enabled = false;
                bnFirst.Enabled = false;
                bnNext.Enabled = false;
                bnLast.Enabled = false;
            }

        }

        private void bnFirst_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            Bind();
            xRefresh();
        }

        private void bnPrevious_Click(object sender, EventArgs e)
        {
            PageIndex -= 1;
            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            Bind();
            xRefresh();
        }

        private void bnNext_Click(object sender, EventArgs e)
        {
            PageIndex += 1;
            if (PageIndex > PageCount)
            {
                PageIndex = PageCount;
            }
            Bind();
            xRefresh();
        }

        private void bnLast_Click(object sender, EventArgs e)
        {
            PageIndex = PageCount;
            Bind();
            xRefresh();
        }

        private void bnGoAppoint_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPage.Text))
            {
                try
                {
                    PageIndex = Convert.ToInt16(txtPage.Text);
                    Bind();
                    xRefresh();
                }
                catch (Exception)
                {
                    MyMessageBox.ShowMessageBoxInfo("格式错误！");

                }
            }else
            {
                MyMessageBox.ShowMessageBoxInfo("指定页面不存在！");
            }
        }

        private void XPagingBar_Load(object sender, EventArgs e)
        {
            Bind();
        }
    }
}
